<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <el-row>
        <el-col>
          <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <tree-table
        ref="table"
        sum-text="sum"
        index-text="#"
        :data="CateList"
        :columns="columns"
        :selection-type="false"
        :expand-type="false"
        show-index
        border
        :show-row-hover="false"
        style="margin-top:15px"
      >
        <!-- 是否有效模板区域 -->
        <!-- eslint-disable-next-line -->
        <template slot="isok" slot-scope="scope">
          <i
            class="el-icon-success"
            v-if="scope.row.cat_deleted === false"
            style="color:lightgreen"
          ></i>
          <i class="el-icon-error" v-else style="color:red"></i>
        </template>
        <!-- 排序模板区域 -->
        <!-- eslint-disable-next-line -->
        <template slot="sort" slot-scope="scope">
          <el-tag v-if="scope.row.cat_level==0" size="mini">一级</el-tag>
          <el-tag type="success" v-if="scope.row.cat_level==1" size="mini">二级</el-tag>
          <el-tag type="warning" v-if="scope.row.cat_level==2" size="mini">三级</el-tag>
        </template>

        <!-- 操作模板区域 -->
        <!-- eslint-disable-next-line -->
        <template slot="opt" slot-scope="scope">
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="showEditDialog(scope.row.cat_id)"
          >编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="removeCateById(scope.row.id)"
          >删除</el-button>
        </template>
      </tree-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[1, 2, 5, 10]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      ></el-pagination>
    </el-card>

    <!-- 新增区域 -->
    <el-dialog
      title="添加分类"
      :visible.sync="addCateDialogVisible"
      width="50%"
      @close="addCateDialogClosed"
    >
      <!-- 内容主题区域 -->
      <el-form :model="addForm" :rules="addFormRules" ref="addFormRef" label-width="100px">
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="addForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="分类等级:">
          <div class="block">
            <span class="demonstration"></span>
            <el-cascader
              v-model="SelectedCates"
              :options="ParentCateList"
              :props="cascaderProps"
              @change="parentCateChanged"
              clearable
            ></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>

    <!-- 编辑区域 -->
    <el-dialog
      title="编辑分类"
      :visible.sync="editCateDialogVisible"
      width="50%"
      @close="editDialogClosed()"
    >
      <!-- 内容主题区域 -->
      <el-form :model="editForm"  ref="editFormRef" label-width="100px">
        <el-form-item label="分类名称:" prop="cat_name">
          <el-input v-model="editForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="分类等级:">
          <div class="block">
            <span class="demonstration"></span>
            <el-cascader
              v-model="SelectedCates"
              :options="ParentCateList"
              :props="cascaderProps"
              @change="parentCateChanged"
              clearable
            ></el-cascader>
          </div>
        </el-form-item>
      </el-form>
      <!-- 底部区 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="editCateDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addCate">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        type: "",
        pagenum: 1,
        pagesize: 5
      },
      total: 0,
      SelectedCates: [],
      CateList: [],
      oneCateList:[],
      ParentCateList: [],
      cascaderProps: {
        value: "cat_id",
        label: "cat_name",
        children: "children",
        expandTrigger: "hover",
        checkStrictly: true
      },
      addCateDialogVisible: false,
      editCateDialogVisible:false,
      addForm: {
        cat_pid: 0,
        cat_name: "",
        cat_level: 0
      },
      editForm:{},
      addFormRules: {
        cat_name: [
          {
            required: true,
            message: "请输入分类名称",
            trigger: "blur"
          },
          {
            min: 2,
            max: 10,
            message: "用户名的长度在2~10个字符之间",
            trigger: "blur"
          }
        ]
      },
      // 为table指定列的定义
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok"
        },
        {
          label: "操作",
          type: "template",
          template: "opt"
        }
      ]
    };
  },
  created() {
    this.getCateList();
  },
  methods: {
    // 获取参数列表
    async getCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: this.queryInfo
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      console.log(res.data.result);
      this.CateList = res.data.result;
      this.total = res.data.total;
    },
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getCateList();
    },
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getCateList();
    },
    addCateDialogClosed() {
      this.$refs.addFormRef.resetFields();
      this.SelectedCates = [];
      this.addForm.cat_pid = 0;
      this.addForm.cat_level = 0;
    },
    editDialogClosed(){
      this.$refs.editFormRef.resetFields();
    },
    showAddCateDialog() {
      this.getParentCateList();
      this.addCateDialogVisible = true;
    },
    // 添加分类
    async addCate() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return;
        //可以发起添加用户的网络请求
        const { data: res } = await this.$http.post("categories", this.addForm);
        if (res.meta.status !== 201) {
          return this.$message.error(res.meta.msg);
        }
        this.$message.success(res.meta.msg);
        console.log(res.data);
        this.addCateDialogVisible = false;
        this.getCateList();
      });
    },
    async getParentCateList() {
      const { data: res } = await this.$http.get("categories", {
        params: { type: 2 }
      });
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.ParentCateList = res.data;
    },
    parentCateChanged() {
      var len = this.SelectedCates.length;
      console.log(this.SelectedCates);
      // 如果SelectedCates的数组长度大于0，则表示选中父级分类，要是等于0，就说明没有选中任何分类
      if (len > 0) {
        this.addForm.cat_level = len;
        this.addForm.cat_pid = this.SelectedCates[len - 1];
      }
    },
    async showEditDialog(id){
      console.log(id)
      const { data: res } = await this.$http.get("categories/"+id);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.editForm = res.data;
      console.log(this.editForm)
      this.editCateDialogVisible = true
    }
  }
};
</script>

<style lang="less" scoped>
.el-cascader {
  width: 100%;
}
</style>